using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

namespace Shrapnel
{
	/// <summary>
	/// A set of selected elements
	/// </summary>
	[DataContract()]
	internal class Selection : ICollection<Element>
	{
		/// <summary>
		/// The actual set of selected elements
		/// </summary>
		[DataMember(Name="Elements")]
		private List<Element> elements = new List<Element>();

		#region ICollection<Element> Members

		public void Add(Element item)
		{
			if (!elements.Contains(item))
			{
				item.IsSelected = true;
				elements.Add(item);
			}
		}

        public void AddRange(ICollection<Element> items)
        {
            foreach (Element item in items)
                Add(item);
        }

		public void Clear()
		{
			foreach (Element element in elements)
			{
				element.IsSelected = false;
			}
			elements.Clear();
		}

		public bool Contains(Element item)
		{
			return elements.Contains(item);
		}

		public void CopyTo(Element[] array, int arrayIndex)
		{
			elements.CopyTo(array, arrayIndex);
		}

		public int Count
		{
			get { return elements.Count; }
		}

		public bool IsReadOnly
		{
			get { return false; }
		}

		public bool Remove(Element item)
		{
			if (elements.Remove(item))
			{
				item.IsSelected = false;
				return true;
			}
			else
			{
				return false;
			}
		}

		#endregion

		#region IEnumerable<Element> Members

		public IEnumerator<Element> GetEnumerator()
		{
			return elements.GetEnumerator();
		}

		#endregion

		#region IEnumerable Members

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return elements.GetEnumerator();
		}

		#endregion
	}
}
